package com.ykdl.member.kid.fragments;

import java.util.ArrayList;
import java.util.List;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.FreakyChatChildListAdapter;
import com.ykdl.member.kid.circle.TopicAndResultActivity;
import com.ykdl.member.kid.gears.DoctorTalkChildListActivity;
import com.ykdl.member.kid.gears.FreakyChatChildListActivity;
import com.ykdl.member.kid.gears.HomeActivity;
import com.ykdl.member.kid.gears.TopicsSheetListActivity;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.models.CatalogArticleItem;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.CatalogItemBean;
import com.ykdl.member.kid.models.CatalogTopicItem;
import com.ykdl.member.kid.models.FileMetas;
import com.ykdl.member.kid.models.HomeBean;
import com.ykdl.member.kid.models.PostActor;
import com.ykdl.member.kid.models.TopicBean;
import com.ykdl.member.kid.util.MobileUtils;
import com.ykdl.member.kid.widget.AsyncImageView;
import com.ykdl.member.views.MyGridView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class FreakyChatChildFragment extends Fragment {
	private HomeActivity activity;
	private MyGridView gv_freaky_chat_child;
	private ArrayList<CatalogBean> catalogs;
	private int screen_width = 0;
	private int gridview_item_width = 0;
	private int gridview_item_image_height = 0;
	private int gridview_width;
	
	private FreakyChatChildListAdapter  adapter;
	private BitmapUtils  bitmapUtil;
	private RelativeLayout rl_freaky_chat_child_title;
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.activity = (HomeActivity) activity;
		bitmapUtil = new BitmapUtils(this.activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = LayoutInflater.from(activity).inflate(
				R.layout.freaky_chat_child_view, null);
		rl_freaky_chat_child_title = (RelativeLayout) view.findViewById(R.id.rl_freaky_chat_child_title);
		
		gv_freaky_chat_child = (MyGridView) view
				.findViewById(R.id.gv_freaky_chat_child);
		
		screen_width = MobileUtils.getScreenWidthIntPx();
		gridview_width = (screen_width - MobileUtils.dpToPx(12));
		gridview_item_width = (screen_width - MobileUtils.dpToPx(12)) / 2;
		gridview_item_image_height = (int)((gridview_item_width - MobileUtils.dpToPx(15)) / 2.8);
		
		return view;

	}

	@SuppressWarnings("unchecked")
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Bundle bundle = this.getArguments();
		// topics = (List<TopicBean>) bundle.getSerializable("topics");
		final HomeBean homeBean = (HomeBean) bundle.getSerializable("homebean");
		catalogs = (ArrayList<CatalogBean>) homeBean.getCatalogs();

		LayoutParams params = (LayoutParams) gv_freaky_chat_child.getLayoutParams();
		params.width = gridview_width;
		params.rightMargin = MobileUtils.dpToPx(6);
		params.leftMargin = MobileUtils.dpToPx(6);
		
		gv_freaky_chat_child.setHorizontalSpacing(MobileUtils.dpToPx(12));
		gv_freaky_chat_child.setVerticalSpacing(MobileUtils.dpToPx(12));
		gv_freaky_chat_child.setColumnWidth(gridview_item_width);
		gv_freaky_chat_child.setCacheColorHint(getResources().getColor(R.color.touming));
		gv_freaky_chat_child.setNumColumns(2);
		
		//设置适配器
		adapter = new FreakyChatChildListAdapter(this.activity, bitmapUtil, gridview_item_image_height);
		adapter.setList(catalogs);
		gv_freaky_chat_child.setAdapter(adapter);
		
		gv_freaky_chat_child.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long arg3) {
				final CatalogBean  catalog = catalogs.get(position);
				Intent  intent = null;
				TopicBean  topic = null;
				
				if(catalog.getCatalog_type() == 1){
					if(catalog.getItems().size() > 1){ //多个话题(进入专题列表页)
						intent =new Intent(activity,TopicsSheetListActivity.class);
						intent.putExtra("catalog_id", catalog.getCatalog_id() + "");
					}else{//单个话题(进入话题详情页)
						if(catalog.getCatalogItems()==null || "[]".equals(catalog.getCatalogItems()) || catalog.getCatalogItems().size() == 0)return;
						topic = ((CatalogTopicItem)catalog.getCatalogItems().get(0)).getItem();
						if(topic !=null){
							intent =new Intent(activity,TopicAndResultActivity.class);
							intent.putExtra(TopicAndResultActivity.STR_TOPIC_ID, topic.getTopic_id());
							intent.putExtra(TopicAndResultActivity.STR_GROUP_ID, topic.getSource_id());
						}else{
							return;
						}
					}
					
					activity.startActivity(intent);
				}
			}

		});
		rl_freaky_chat_child_title.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent t = new Intent(activity,  FreakyChatChildListActivity.class);
				startActivity(t);
			}
		});

	}

}
